package lumis.service.chatroom;

import java.util.Collection;

import lumis.collaboration.chat.ChatConversationConfig;
import lumis.collaboration.chat.ChatManagerFactory;
import lumis.collaboration.chat.ChatMessageConfig;
import lumis.collaboration.chat.ChatUserConfig;
import lumis.content.ContentManagerFactory;
import lumis.content.core.ContentVersion;
import lumis.doui.source.IDataProvider;
import lumis.doui.source.ISourceData;
import lumis.doui.source.TabularData;
import lumis.doui.source.TabularSource;
import lumis.portal.PortalException;
import lumis.portal.PortalObjectNotFoundException;
import lumis.portal.PortalRequestParameters;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.dao.hibernate.ITransactionHibernate;
import lumis.portal.user.UserConfig;
import lumis.util.ITransaction;

import org.hibernate.Session;

/**
 * Provides a list of messages in a chat room.
 *
 * @version $Revision: 8896 $ $Date: 2008-02-19 12:57:12 -0300 (Tue, 19 Feb 2008) $
 * @since 4.0.11
 */
public class ChatRoomHistoryDataProvider implements IDataProvider<TabularSource<?>>
{
	public void loadData(SessionConfig sessionConfig, TabularSource<?> source, ITransaction transaction) throws PortalException
	{
		String chatRoomContentId = source.getDouiContext().getRequest().getParameter(PortalRequestParameters.PAGE_PARAMETER_ITEM_ID);
		if (chatRoomContentId == null)
			throw new IllegalArgumentException(PortalRequestParameters.PAGE_PARAMETER_ITEM_ID + " not found");

		// get chatRoomId
		ContentVersion chatRoomContentVersion = ContentManagerFactory.getContentManager().getPublishedContentVersion(
				chatRoomContentId, source.getSourceContext().getServiceInstanceConfig().getId());
		String chatRoomId = chatRoomContentVersion.getItemId();
		
		ITransactionHibernate transactionHib = (ITransactionHibernate) transaction;
		Session session = transactionHib.getSession();
		ChatRoomConfig chatRoomConfig = (ChatRoomConfig)session.get(ChatRoomConfig.class, chatRoomId);
		if(chatRoomConfig == null)
			throw new IllegalArgumentException("ChatRoom with id='" + chatRoomId + "' Id not found");
		
		String chatConversationId = chatRoomConfig.getChatConversationId();
	
		// get the chat user for the current portal user
		ChatUserConfig chatUserConfig = null;
		String userSessionId = sessionConfig.getUserSessionId();
		String userId = sessionConfig.getUserId();
		if(userId.equals(UserConfig.USER_GUEST_ID))
		{
			// get the chat user for this session
			try
			{
				chatUserConfig = ChatManagerFactory.getChatManager().getUser(sessionConfig, userId, chatConversationId, userSessionId, transaction);
			}
			catch(PortalObjectNotFoundException e)
			{
				
			}
		}
		else
		{
			// get the chat user for this userId
			try
			{
				chatUserConfig = ChatManagerFactory.getChatManager().getUser(sessionConfig, userId, chatConversationId, null, transaction);
			}
			catch(PortalObjectNotFoundException e)
			{
				
			}
		}
	
		String chatUserId = null;
		if(chatUserConfig!=null)
			chatUserId = chatUserConfig.getId();
		
		ChatConversationConfig chatConversation = ChatManagerFactory.getChatManager().getConversation(sessionConfig, chatConversationId, transaction);
		
		Collection<ChatMessageConfig> messageConfigs = ChatManagerFactory.getChatManager().getLatestMessages(sessionConfig, chatConversationId, 
				chatUserId, source.getStartAt()-1, source.getMaxRows(), false, null, transaction);
		
		TabularData tabularData = (TabularData) source.getData();
		ISourceData row = tabularData.addRow();
		row.put("messages", messageConfigs);
		
		int totalRows = ChatManagerFactory.getChatManager().getLatestMessagesCount(sessionConfig, chatConversation.getId(), chatUserId, false, null, transaction);
		tabularData.setTotalRows(totalRows);
	}
}
